package wechat.base.snd;

import java.util.Collection;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.json.JSONArray;
import wechat.base.snd.WCApplication;
import wechat.base.snd.WCMessage;
import wechat.base.snd.WCNewsItem;
import wechat.base.snd.encoding.JsonPut;

@SuppressWarnings("all")
public class WCNews extends WCMessage {
  public WCNews(final WCApplication application) {
    super(application);
    this.encoding.setMsgType("news");
  }
  
  private Collection<WCNewsItem> items = CollectionLiterals.<WCNewsItem>newArrayList();
  
  public WCNewsItem withItem(final String title) {
    WCNewsItem _wCNewsItem = new WCNewsItem(title);
    final Procedure1<WCNewsItem> _function = new Procedure1<WCNewsItem>() {
      public void apply(final WCNewsItem it) {
        WCNews.this.items.add(it);
      }
    };
    return ObjectExtensions.<WCNewsItem>operator_doubleArrow(_wCNewsItem, _function);
  }
  
  public void buildContents() {
    final JsonPut contents = new JsonPut();
    final JSONArray ja = new JSONArray();
    for (final WCNewsItem obj : this.items) {
      ja.put(obj.toJson());
    }
    final Procedure1<JsonPut> _function = new Procedure1<JsonPut>() {
      public void apply(final JsonPut it) {
        it.putObj("articles", ja);
      }
    };
    ObjectExtensions.<JsonPut>operator_doubleArrow(contents, _function);
    this.encoding.setNews(contents);
  }
}
